using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace Wrath.RuleForge.Imaging
{
    public partial class FrameEntryBox : UserControl
    {
        private DialogResult dialogResult = DialogResult.None;
        public DialogResult DialogResult
        {
            get { return dialogResult; }
        }

        public event EventHandler CancelFrame;
        public event EventHandler SetFrame;

        private FrameGuideControl.Rectangle frame = null;
        public FrameGuideControl.Rectangle Frame
        {
            get { return frame; }
            set { SetValues(value); }
        }

        private void SetValues(FrameGuideControl.Rectangle newFrame)
        {
            if (newFrame != null)
            {
                newFrame.CopyTo(ref frame);
            }
            else
                frame = newFrame;

            if (frame != null)
            {
                textBoxLeft.Text = frame.Left.ToString();
                textBoxTop.Text = frame.Top.ToString();
                textBoxWidth.Text = frame.Width.ToString();
                textBoxHeight.Text = frame.Height.ToString();

                buttonSetFrame.Enabled = true;
            }
            else
            {
                textBoxLeft.Text = "";
                textBoxTop.Text = "";
                textBoxWidth.Text = "";
                textBoxHeight.Text = "";

                buttonSetFrame.Enabled = false;
            }
        }

        public FrameEntryBox()
        {
            InitializeComponent();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            dialogResult = DialogResult.Cancel;

            if (CancelFrame != null)
                CancelFrame(this, new EventArgs());
        }

        private void button1_Click(object sender, EventArgs e)
        {
            dialogResult = DialogResult.OK;

            frame = new FrameGuideControl.Rectangle();
            frame.X = Convert.ToInt32(textBoxLeft.Text);
            frame.Y = Convert.ToInt32(textBoxTop.Text);
            frame.Width = Convert.ToInt32(textBoxWidth.Text);
            frame.Height = Convert.ToInt32(textBoxHeight.Text);

            if (SetFrame != null)
                SetFrame(this, new EventArgs());
        }

        private void textBox_KeyDown(object sender, KeyEventArgs e)
        {

            if (e.KeyCode != Keys.D0 &&
                e.KeyCode != Keys.D1 &&
                e.KeyCode != Keys.D2 &&
                e.KeyCode != Keys.D3 &&
                e.KeyCode != Keys.D4 &&
                e.KeyCode != Keys.D5 &&
                e.KeyCode != Keys.D6 &&
                e.KeyCode != Keys.D7 &&
                e.KeyCode != Keys.D8 &&
                e.KeyCode != Keys.D9 &&
                e.KeyCode != Keys.Delete &&
                e.KeyCode != Keys.Back &&
                e.KeyCode != Keys.Left &&
                e.KeyCode != Keys.Right)
            {
                e.SuppressKeyPress = true;
            }
        }
    }
}
